// created on 25/03/2006 at 23:11
using System;
using Gtk;
using GtkSharp;
using Hildon;

public class NumberOfPlayers : Gtk.Dialog
{
	private Entry[] players=new Entry[4];
	private Combo[] playersType=new Combo[4];
	private String[] playerName=new String[4];
	private String[] playerType=new String[4];
	private bool _confirmPlayers=false;
	private int _numberOfPlayers;
	private Hildon.NumberEditor ne=null;
	
	public bool ConfirmPlayers {
		get { return _confirmPlayers; }
	} 
	 
	public int PlayersNumber {
		get { return _numberOfPlayers; }
	}
	
	public String GetPlayer(int playerNumber) {
		return playerName[playerNumber]; 
	} 
	 
	public String GetPlayerType(int playerNumber) {
		return playerType[playerNumber]; 
	} 

	public ComputerGamersInterface CreatePlayer(int playerNumber) {
		ComputerGamersInterface ret=null;
		for (int j=0; j<PluginsManager.numberOfPlugin(); j++) {
			if (playerType[playerNumber].Equals(PluginsManager.getComputerGamesClassName(j))) {
				ret=PluginsManager.createNewInstance(j);
				break;
			}
		}

		return ret; 
	} 

	public NumberOfPlayers ()
	{

		this.Title = "Number of Players";

		// Create VBox to contains the several parts of the interface
		VBox.Spacing=6;

		// Create the region where insert the number of players			
		Table tableLayout=new Table(4,1,false);
		tableLayout.Attach(new Label("Enter number of Players:"),1,2,0,1);

		ne=new Hildon.NumberEditor(1,4);
		tableLayout.Attach(ne,2,3,0,1);

		ne.AddNotification ("value", spinButtonValueChanged);
	
		VBox.Add(tableLayout);

		// Create the list of players
		bool sensitive=true;
		tableLayout=new Table(3,4,false);
		tableLayout.SetColSpacing(1,10);
		for (uint i=1; i<5; i++) {
			players[i-1]=new Entry("Player "+i);
			Label label=new Label("Enter the name of player "+i);
			playersType[i-1]=new Combo();
			playersType[i-1].ValueInList=true;

			string[] list;
			list=new string[PluginsManager.numberOfPlugin()+1];
			list[0]= "Human";
			for (int j=0; j<PluginsManager.numberOfPlugin(); j++) {
				list[j+1]=PluginsManager.getPluginsDefaultNamePlayer(j);
			}

			playersType[i-1].PopdownStrings=list;
			label.Xpad=10;
			label.Ypad=10;
			tableLayout.Attach(label,0,1,i-1,i);
			tableLayout.Attach(players[i-1],1,2,i-1,i);
			tableLayout.Attach(playersType[i-1],2,3,i-1,i);
			players[i-1].Sensitive=sensitive;
			playersType[i-1].Sensitive=sensitive;
			sensitive=false;
		}
		
		VBox.Add(tableLayout);

		// Create the region where insert Push Button 			
		Button buttonOk=new Button("gtk-ok");
		buttonOk.UseStock=true;
				
		Button buttonCancel=new Button("gtk-cancel");
		buttonCancel.UseStock=true;
		buttonOk.Clicked += new EventHandler(buttonOkCliked);
		buttonCancel.Clicked += new EventHandler(buttonCancelCliked);
		
		AddActionWidget(buttonOk,0);
		AddActionWidget(buttonCancel,1);
	}
	
	public void spinButtonValueChanged(object o, EventArgs args) {
	
		NumberEditor ne=(NumberEditor)o;

		// Get the SpinButton value
		int nPlayers = ne.Value;
		
		// Set the sensitive of the Entries
		for (uint i=0; i<nPlayers; i++) {
			players[i].Sensitive=true;
			playersType[i].Sensitive=true;
		}
		for (uint i=(uint)nPlayers; i<4; i++) {
			players[i].Sensitive=false;
			playersType[i].Sensitive=false;
		}

	}

	public void buttonOkCliked(object o, EventArgs args) {
		_confirmPlayers=true;
		_numberOfPlayers=ne.Value;
		for (int i=0; i<_numberOfPlayers; i++) {
			playerName[i]=players[i].Text;
			playerType[i]=playersType[i].Entry.Text;

			// Loop to fine the correct class of player
			for (int j=0; j<PluginsManager.numberOfPlugin(); j++) {
				if (playersType[i].Entry.Text.Equals(PluginsManager.getPluginsDefaultNamePlayer(j))) {
					playerType[i]=PluginsManager.getComputerGamesClassName(j);
					break;
				}
			}
		}
		
		this.Hide();
	}

	public void buttonCancelCliked(object o, EventArgs args) {
		_confirmPlayers=false;
		this.Hide();
	}
	
}

